﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式之禅.命令模式
{
    /// <summary>
    /// 抽象命令角色
    /// </summary>
    public abstract class AbstractCommand
    {
        public abstract string Execute();
    }

    /// <summary>
    /// 具体命令角色A
    /// </summary>
    public class CommandA : AbstractCommand
    {
        private AbstractReceiver _receiver;
        public CommandA(AbstractReceiver receiver)
        {
            this._receiver = receiver;
        }
        public override string Execute()
        {
            return this._receiver.doSomething();
        }
    }
    /// <summary>
    /// 具体命令角色B
    /// </summary>
    public class CommandB : AbstractCommand
    {
        private AbstractReceiver _receiver;
        public CommandB(AbstractReceiver receiver)
        {
            this._receiver = receiver;
        }
        public override string Execute()
        {
            return this._receiver.doSomething() + this._receiver.doSomething();
        }
    }

    /// <summary>
    /// 抽象接收者
    /// </summary>
    public abstract class AbstractReceiver
    {
        public abstract string doSomething();
    }
    public class ReceiverA : AbstractReceiver
    {
        public override string doSomething()
        {
            return "ReceiverA";
        }
    }
    public class ReceiverB : AbstractReceiver
    {
        public override string doSomething()
        {
            return "ReceiverB";
        }
    }

    /// <summary>
    /// 调用者角色
    /// </summary>
    public class Invoker
    {
        private AbstractCommand _command;
        public void SetCommand(AbstractCommand commad)
        {
            this._command = commad;
        }
        public string Action()
        {
            return this._command.Execute();
        }
    }
}
